<?php
/**
 * V5
 *
 */
class data_feed_folder extends data {
	public function __construct() {
		parent::__construct();
	}
	public function feed_each_other ($uid, $fid) {
		$uid	= intval($uid);
		$fid	= intval($fid);
		
		if ($uid and $fid) {
			$this->feed_to_friend($uid, $fid);
			$this->feed_to_friend($fid, $uid);
		} else return false;
	}
	/**
	 * 一个用户加另一个用户为好友时，订阅他public文件夹的feed，并复制以前的feed过来
	 *
	 * @param int $uid
	 * @param int $fid
	 * @return true
	 */
	private function feed_to_friend ($uid, $fid) {
		$follow_folder = $this->db->execute('
			insert into
				feed_to_folder
			(user_id, folder_id, friend_id)
				select
					friend.user_id,
					save_folder.id,
					friend.friend_id
				from
					friend, save_folder
				where
					friend.user_id			= "'.$uid.'"			and
					friend.friend_id		= "'.$fid.'"			and
					save_folder.user_id		= friend.friend_id		and
					save_folder.privacy 	= "public"
		');
		// 复制feed
		$data_feed = new data_feed();
		$copy_feed = $data_feed->copy_feed($uid, $fid);
		
		return $follow_folder or $copy_feed;
	}
	/**
	 * 当取消好友关系时，删除互相的feed订阅
	 *
	 * @param int $uid
	 * @param int $fid
	 * @return true
	 */
	public function unfeed_each_other ($uid, $fid) {
		$uid	= intval($uid);
		$fid	= intval($fid);
		
		if ($uid and $fid) {
			$del = $this->db->execute('
				delete from
					feed_to_folder
				where
					(user_id = "'.$uid.'" and friend_id = "'.$fid.'") 
					or
					(user_id = "'.$fid.'" and friend_id = "'.$uid.'")
			');
			$del_feed = $this->db->execute('
				delete from
					feed
				where
					(sender_id = "'.$uid.'" and receiver_id = "'.$fid.'") 
					or
					(receiver_id = "'.$fid.'" and sender_id = "'.$uid.'")
			');
			return true;
		} else return false;
	}
	/**
	 * “我”去主动关注某个好友的folder
	 * 			检查folder的主人是否与我是好友关系
	 * 			folder必须是public
	 * 			没有复制feed
	 *
	 * @param int $uid
	 * @param int $folder_id
	 * @return int
	 */
	public function feed_one_folder ($uid, $folder_id) {
		$uid		= intval($uid);
		$folder_id	= intval($folder_id);
		
		if ($uid and $folder_id) {
			$feed = $this->db->execute('
				insert into
					feed_to_folder
				(user_id, folder_id, friend_id)
					select
						friend.user_id,
						save_folder.id,
						save_folder.user_id
					from
						save_folder, friend
					where
						save_folder.id			= "'.$folder_id.'"			and
						save_folder.privacy		= "public"					and
						friend.user_id			= "'.$uid.'"				and
						friend.friend_id		= save_folder.user_id
			');
			return $feed;
		} else return false;
	}
	/**
	 * “我”不再接收此folder的feed信息
	 *
	 * @param int $uid
	 * @param int $folder_id
	 * @return int
	 */
	public function unfeed_one_folder ($uid, $folder_id) {
		$uid		= intval($uid);
		$folder_id	= intval($folder_id);
		
		if ($uid and $folder_id) {
			$del = $this->db->execute('
				delete from
					feed_to_folder
				where
					user_id		= "'.$uid.'"		and
					folder_id	= "'.$folder_id.'"
			');
			$del_feed = $this->db->execute('
				delete from
					feed
				where
					receiver_id		= "'.$uid.'"		and
					source_id		IN (select id from save_article where folder_id = "'.$folder_id.'")	and
					type			= "save_article"
			');
			return $del;
		} else return false;
	}
	/**
	 * 文件夹是否已经接受feed
	 *
	 * @param int $uid
	 * @param int $folder_id
	 * @return bool
	 */
	public function is_feeded ($uid, $folder_id) {
		$uid		= intval($uid);
		$folder_id	= intval($folder_id);
		
		if ($uid and $folder_id) {
			$execute = $this->db->execute('
				select
					id
				from
					feed_to_folder
				where
					user_id		= "'.$uid.'"		and
					folder_id	= "'.$folder_id.'"
			');
			if ($execute[0]['id']) {
				return true;
			} else return false;
		} else return false;
	}
}
?>